package com.minsf.springbootinit.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.minsf.springbootinit.model.dto.question.JudgeCase;
import com.minsf.springbootinit.model.dto.question.JudgeConfig;
import com.minsf.springbootinit.judge.codesandbox.model.JudgeInfo;
import com.minsf.springbootinit.model.entity.Question;
import com.minsf.springbootinit.model.enums.QuestionSubmitStatusEnum;

import java.util.List;
import java.util.Optional;

/**
 * @Description
 * @Author minify
 * @Date 2024/3/9 19:13
 * @Project oj-backend
 */
public class JavaJudgeStrategyImpl implements JudgeStrategy {
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        QuestionSubmitStatusEnum judgmentOfQuestion = QuestionSubmitStatusEnum.JUDGMENT_SUCCESS;
        try {
            JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
            List<String> inputList = judgeContext.getInputList();
            List<String> outputList = judgeContext.getOutputList();
            Question question = judgeContext.getQuestion();
            List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();

            // 判断题目限制

            Integer memory = Optional.ofNullable(judgeInfo.getMemory()).orElse(0);
            Integer time = Optional.ofNullable(judgeInfo.getTime()).orElse(0);

            judgeInfoResponse.setTime(time);
            judgeInfoResponse.setMemory(memory);

            if (outputList.size() != inputList.size()) {
                judgmentOfQuestion = QuestionSubmitStatusEnum.JUDGMENT_FAILED;
                judgeInfoResponse.setMessage(judgmentOfQuestion.getName());
                return judgeInfoResponse;
            }
            for (int i = 0; i < judgeCaseList.size(); i++) {
                JudgeCase judgeCase1 = judgeCaseList.get(i);
                if (!judgeCase1.getOutput().equals(outputList.get(i))) {
                    judgmentOfQuestion = QuestionSubmitStatusEnum.JUDGMENT_FAILED;
                    judgeInfoResponse.setMessage(judgmentOfQuestion.getName());
                    return judgeInfoResponse;
                }
            }

            String judgeConfigStr = question.getJudgeConfig();
            JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
            Long memoryLimit = judgeConfig.getMemoryLimit();
            Long timeLimit = judgeConfig.getTimeLimit();

            if (memory > memoryLimit) {
                judgmentOfQuestion = QuestionSubmitStatusEnum.JUDGMENT_FAILED;
                judgeInfoResponse.setMessage(judgmentOfQuestion.getName());
                return judgeInfoResponse;
            }
            // 加入Java本身需要多执行10s
            long JAVA_PROGRAM_TIME_COST = 1000L;
            if (time - JAVA_PROGRAM_TIME_COST > timeLimit) {
                judgmentOfQuestion = QuestionSubmitStatusEnum.JUDGMENT_FAILED;
                judgeInfoResponse.setMessage(judgmentOfQuestion.getName());
                return judgeInfoResponse;
            }
        } catch (Exception e) {
            judgmentOfQuestion= QuestionSubmitStatusEnum.JUDGMENT_FAILED;
            judgeInfoResponse.setMessage(judgmentOfQuestion.getName());
            return judgeInfoResponse;
        }

        return judgeInfoResponse;
    }
}
